/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package test.integ.be.fedict.hsm.model;

import java.util.UUID;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import be.fedict.hsm.entity.KeyStoreEntity;
import be.fedict.hsm.entity.KeyStoreType;
import be.fedict.hsm.model.KeyStoreSingletonBean;

@Path("/test")
@Stateless
public class ConcurrencyTestController {

	private static final Log LOG = LogFactory
			.getLog(ConcurrencyTestController.class);

	@EJB
	private KeyStoreTestBean keyStoreTestBean;

	@EJB
	private KeyStoreSingletonBean keyStoreSingletonBean;

	@GET
	@Path("/init")
	@Produces(MediaType.APPLICATION_XML)
	public InitResult init() {
		LOG.debug("init");
		long eTokenKeyStoreId = initEToken();
		long beIdkeyStoreId = initBeID();

		return new InitResult(Long.toString(eTokenKeyStoreId),
				Long.toString(beIdkeyStoreId));
	}

	private long initEToken() {
		KeyStoreEntity eTokenKeyStoreEntity = new KeyStoreEntity(UUID
				.randomUUID().toString(), KeyStoreType.PKCS11,
				"/usr/lib/libeTPkcs11.so", "HSMProxy1234", 0);
		this.keyStoreTestBean.store(eTokenKeyStoreEntity);
		long eTokenKeyStoreId = eTokenKeyStoreEntity.getId();
		LOG.debug("eToken key store id: " + eTokenKeyStoreId);
		this.keyStoreSingletonBean.newKeyStore(eTokenKeyStoreId);
		return eTokenKeyStoreId;
	}

	private long initBeID() {
		KeyStoreEntity beIdKeyStoreEntity = new KeyStoreEntity(UUID
				.randomUUID().toString(), KeyStoreType.PKCS11,
				"/usr/lib/libbeidpkcs11.so.0", null, 1);
		this.keyStoreTestBean.store(beIdKeyStoreEntity);
		long beIdKeyStoreId = beIdKeyStoreEntity.getId();
		LOG.debug("BeID key store id: " + beIdKeyStoreId);
		this.keyStoreSingletonBean.newKeyStore(beIdKeyStoreId);
		return beIdKeyStoreId;
	}

	@GET
	@Path("/beid")
	public void beId() {
		LOG.debug("beid: begin");
		try {
			this.keyStoreSingletonBean.sign(2, "Authentication", "SHA-1",
					new byte[20]);
		} catch (Exception e) {
			LOG.error("BeID sign error: " + e.getMessage(), e);
		}
		LOG.debug("beid: end");
	}

	@GET
	@Path("/etoken")
	public void eToken() {
		LOG.debug("etoken: begin");
		try {
			this.keyStoreSingletonBean.sign(1, "HSM", "SHA-1", new byte[20]);
		} catch (Exception e) {
			LOG.error("eToken sign error: " + e.getMessage(), e);
		}
		LOG.debug("etoken: end");
	}

	@XmlRootElement(name = "keystores")
	@XmlAccessorType(XmlAccessType.FIELD)
	public static class InitResult {
		@XmlElement(required = true)
		private String eToken;
		@XmlElement(required = true)
		private String beID;

		public InitResult() {
			super();
		}

		public InitResult(String eToken, String beID) {
			this.eToken = eToken;
			this.beID = beID;
		}
	}
}
